<?php
/**
 * Created by PhpStorm.
 * User: geolo
 * Date: 2018/11/23
 * Time: 14:50
 */
namespace app\controllers;

use app\models\User;
use app\models\VipOrder;
use app\models\Store;
use app\models\WechatApp;
use luweiss\wechat\DataTransform;
use luweiss\wechat\Wechat;


class PayNotifyController extends Controller
{
    public $enableCsrfValidation = false;


    //123456
    public function actionIndex()
    {
        $xml = file_get_contents("php://input");
        $res = DataTransform::xmlToArray($xml);
        if ($res && !empty($res['out_trade_no'])) {//微信支付回调
            $this->wechatPayNotify($res);
        }
    }

    private function wechatPayNotify($res)
    {
        if ($res['result_code'] != 'SUCCESS' && $res['return_code'] != 'SUCCESS')
            return;

        $order = VipOrder::findOne([
            'order_no' => $res['out_trade_no'],
        ]);


        $store = Store::findOne($order->store_id);
        if (!$store)
            return;
        $wechat_app = WechatApp::findOne($store->wechat_app_id);
        if (!$wechat_app)
            return;
        $wechat = new Wechat([
            'appId' => $wechat_app->app_id,
            'appSecret' => $wechat_app->app_secret,
            'mchId' => $wechat_app->mch_id,
            'apiKey' => $wechat_app->key,
            'cachePath' => \Yii::$app->runtimePath . '/cache',
        ]);
        $new_sign = $wechat->pay->makeSign($res);
        if ($new_sign != $res['sign']) {
            echo "Sign 错误";
            return;
        }
        if ($order->is_pay == 1) {
            echo "订单已支付";
            return;
        }
        $order->is_pay = 1;
        $order->create_time = time();
        $order->pay_type = 1;
        if ($order->save()) {
           $user = User::findOne(['store_id'=>$order->store_id,'id'=>$order->user_id]);
           if(!$user){
               $user->is_vip =1;
               if($user->save()){
                   echo '成功';
               }
           }else{
               echo "不存在该用户";
               return;
           }
            echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
            return;
        } else {
            echo "支付失败";
            return;
        }
    }

  }